#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :jingdian_info.py
# @Time      :2023/10/30 
# @Author    :CL
# @email     :1037654919@qq.com
# 解析一个具体景点的信息
    # 概述
    # 门票
    # 旅游时节
    # 交通指南
    # 小贴士
    # 驴友点评(32606)
import time
import requests
from bs4 import  BeautifulSoup
from retrying import retry

from utils import mongo_manager,get_kuai_proxy

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Pragma": "no-cache",
    "Proxy-Connection": "keep-alive",
    "Referer": "http://travel.qunar.com/place/?from=header",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
cookies = {
    "QN1": "0000c180306c57ae8cf8d525",
    "QN300": "s%3Dgoogle",
    "QN99": "8159",
    "QunarGlobal": "10.76.34.188_476f0ecd_18b7f08d2d0_-316c|1698646943873",
    "qunar-assist": "{%22version%22:%2220211215173359.925%22%2C%22show%22:false%2C%22audio%22:false%2C%22speed%22:%22middle%22%2C%22zomm%22:1%2C%22cursor%22:false%2C%22pointer%22:false%2C%22bigtext%22:false%2C%22overead%22:false%2C%22readscreen%22:false%2C%22theme%22:%22default%22}",
    "QN205": "s%3Dgoogle",
    "QN277": "s%3Dgoogle",
    "csrfToken": "bLP49THyP6qyD4ZpvJopVkZIYwMJQoeX",
    "_i": "DFiEuZkoc2SDZ7nLtuc9nvvIcDSw",
    "_vi": "bY-tZiihTo6CKdkOwu63r3O2Q5nL1XjhId_CIR7ATlhPum3BwpbJzbYgj-Zd8jv7asv0eLxnMbKYrTC7yxX-PHPu1n9KxN4H30BUM6iloX3wlgMSm8x68P4eLeWVV_TcE5XsmBZpt-EMQUanqeeZ_2wYEgOzTOztTxWrsVceBudR",
    "QN269": "B1AEDAD076EC11EEBD98FA163EC565DF",
    "QN601": "1844dcac9a8193dff4a2197440799d4c",
    "QN163": "0",
    "fid": "b781e0b7-f523-4a6c-b67d-c9266a2dc911",
    "QN48": "tc_f450c1fd5a4f469f_18b7f3f743c_83bc",
    "ariaDefaultTheme": "null",
    "QN57": "16986471185280.9089655108923953",
    "QN58": "1698647118527%7C1698647185294%7C4",
    "QN5": "qunar_djmp_gnmdd_%E6%AD%A6%E6%B1%89",
    "Hm_lvt_c56a2b5278263aa647778d304009eafc": "1698647830",
    "viewpoi": "9209153",
    "uld": "1-368750-1-1698647833",
    "JSESSIONID": "85078BF4AA0A2D3465CA7EC0B0617483",
    "QN267": "02836690579c3fee1",
    "QN271": "22d90c5a-cc91-4910-96eb-b7e945f2db95",
    "Hm_lpvt_c56a2b5278263aa647778d304009eafc": "1698647834",
    "SECKEY_ABVK": "NmQ3/dWPJQCKo0At1o15ePnB/2lENq4hJk7JijwRBRQ%3D",
    "BMAP_SECKEY": "NmQ3_dWPJQCKo0At1o15eJFgIPnef78cKZVulnYF8GHjsZIfh6rNKjvWMhqiKMZNhm6Hy8mCGzZmMBHQN709hrLE1ZHS-rW8BspQ7vnNaWJT9HVrqMKZvrb3cdLQNH28HOVwlleXHrsDaq9FJ2iFK7xoBf0CFpndAK6nu-4hXO2lABidIKdhTqEZCrt1nRIe"
}
def print_sight_info(url = "http://travel.qunar.com/p-oi715901-gulangyu"):

    response = requests.get(url, headers=headers, cookies=cookies, verify=False)

    # print(response.text)
    print(response)
    soups =BeautifulSoup(response.text, 'lxml')
    datas =soups.find('div', class_='b_detail_nav').find_all('li')
    for data in datas:
        print(data.text)
    datas =soups.find('div', class_='b_detail_section_box').find_all('div',recursive=False)
    for data in datas:
        # print(data['class'])
        if len(data['class']) ==2:
            print(data['class'][1])
            if data['class'][1] =='b_detail_summary':
                print(data.text.replace('\n\n', ''))
            elif data['class'][1] =='b_detail_ticket':
                e_ticket_info =  data.find('div', class_='e_ticket_info')
                clrfix_elements = e_ticket_info.find_all('dl',class_='clrfix')
                lens = min(10,len(clrfix_elements)-1)
                for element in clrfix_elements[:lens]:
                    # print(element.prettify())
                    print(str(element.text).replace('\n','\t'))
            elif data['class'][1] in ['b_detail_travelseason','b_detail_traffic','b_detail_tips']:
                print(data.find('h3').text)
                pp = data.find_all('p')
                for p in pp:
                    print(p.text)
                # print(data.text.strip())

            elif data['class'][1] =='b_detail_comment':
                print(data.find('h3').text.strip())
                score = data.find('div', class_='score').text
                print(str(score).strip())

@retry(stop_max_attempt_number = 3)
def get_jingdian_info(url):
    response = requests.get(url, headers=headers, proxies=get_kuai_proxy(), cookies=cookies, timeout=10, verify=False)

    if response.status_code == 200:
        return response.text
def main():
    while True:
        qunar_sight_info = mongo_manager("qunar_sight_info", db='lvyou')
        seeds = qunar_sight_info.findAll({'status':None}).limit(100)
        lists=[]
        for seed in seeds:
            lists.append(seed)
        print(len(lists))
        for seed in lists:
            url = seed['href']
            response = get_jingdian_info(url)
            print(url)
            if response:
                soups = BeautifulSoup(response, 'lxml')
                try:
                    datas = soups.find('div', class_='b_detail_section_box').find_all('div', recursive=False)
                    for data in datas:
                        if len(data['class']) == 2:
                            # print(data['class'][1])
                            if data['class'][1] == 'b_detail_summary':
                                # print(data.text.replace('\n\n', ''))
                                seed[data['class'][1]] =  data.text.replace('\n\n', '')
                            elif data['class'][1] == 'b_detail_ticket':
                                e_ticket_infos = []
                                try:
                                    clrfix_elements = data.find('div', class_='e_ticket_info').find_all('dl', class_='clrfix')
                                    lens = min(10, len(clrfix_elements) - 1)
                                    for element in clrfix_elements[:lens]:
                                        # print(str(element.text).replace('\n', '\t'))
                                        e_ticket_infos.append(str(element.text).replace('\n', '\t'))
                                except BaseException as e:
                                    e_ticket_infos.append(data.text.strip())
                                    print(url,e)
                                seed[data['class'][1]] = e_ticket_infos
                            elif data['class'][1] in ['b_detail_travelseason', 'b_detail_traffic', 'b_detail_tips']:
                                infos = ""
                                # print(data.find('h3').text)
                                infos +=data.find('h3').text +'\n'
                                pp = data.find_all('p')
                                for p in pp:
                                    # print(p.text)
                                    infos +='\t'+p.text+'\n'
                                seed[data['class'][1]] = infos
                            elif data['class'][1] == 'b_detail_comment':
                                infos= ''
                                # print(data.find('h3').text.strip())
                                infos +=data.find('h3').text.strip()+'\n'
                                score = data.find('div', class_='score').text
                                # print(str(score).strip().replace('\n', '\t'))
                                infos +=str(score).strip().replace('\n', '\t')
                                seed[data['class'][1]] = infos
                    seed['status'] ='success'
                    qunar_sight_info.updateOne({"_id":seed["_id"]},seed)
                except:
                    pass
        qunar_sight_info.close()
        if len(lists) < 100:
            break
        time.sleep(10)


if __name__ == "__main__":
    print()
    # print_sight_info()
    main()


